package com.openvalley.cyj.parse.getter.slice;

import com.openvalley.cyj.parse.getter.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Text;
import ohos.agp.window.dialog.ToastDialog;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.miscservices.pasteboard.PasteData;
import ohos.miscservices.pasteboard.SystemPasteboard;

public class MainAbilitySlice extends AbilitySlice {
    private static final HiLogLabel LOG_MAIN = new HiLogLabel(HiLog.LOG_APP, 0x00001, MainAbilitySlice.class.getSimpleName());
    //拿到系统的剪切板操作接口
    SystemPasteboard systemPasteboard = null;
    Text text_paste = null;
    Text text_show = null;
    Text text_clear = null;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        text_show = (Text)findComponentById(ResourceTable.Id_text_show);

        text_paste = (Text)findComponentById(ResourceTable.Id_text_paste);
        text_paste.setClickedListener(listen -> doPaste());

        text_clear = (Text)findComponentById(ResourceTable.Id_text_clear);
        text_clear.setClickedListener(listen -> doClear());

        systemPasteboard = SystemPasteboard.getSystemPasteboard(getContext());
    }

    /**
     * 清空剪切板所有内容
     */
    private void doClear() {
        if(systemPasteboard != null){
            //Clears the pasteboard. <- 官方文档解释
            //从日志推断，把systemPasteboard中的数据对象（通俗点就是平时我们长按粘贴的内容）清除了
            systemPasteboard.clear();
            //顺便界面也清了
            text_show.setText("等待粘贴");
        }
    }

    /**
     * 获得系统剪切板的内容
     */
    private void doPaste() {
        if(systemPasteboard != null){
            //如果系统的剪切板操作接口不为空才能拿数据对象
            HiLog.info(LOG_MAIN, "systemPasteboard.hasPasteData() = " + systemPasteboard.hasPasteData());
            if(systemPasteboard.hasPasteData()){
                //判断剪切板操作接口中是否有数据对象再创建数据对象
                //快捷粘贴都是粘贴的剪切板中的最近的一条数据对象，这里同理。
                StringBuilder stringBuilder = new StringBuilder();
                PasteData pasteData = systemPasteboard.getPasteData();
                //获得数据对象中有多少个内容条目
                int count = pasteData.getRecordCount();
                //循环遍历内容条目
                for(int i = 0; i < count; i++){
                    PasteData.Record record = pasteData.getRecordAt(i);
                    String mimeType = record.getMimeType();
                    if(PasteData.MIMETYPE_TEXT_PLAIN.equals(mimeType)){
                        HiLog.info(LOG_MAIN, "mimeType = MIMETYPE_TEXT_PLAIN");
                        //文本类型
                        stringBuilder.append(record.getPlainText());
                        if(i != count - 1){
                            stringBuilder.append("\n");
                        }
                    }else if(PasteData.MIMETYPE_TEXT_HTML.equals(mimeType)){
                        HiLog.info(LOG_MAIN, "mimeType = MIMETYPE_TEXT_HTML");
                        //html类型
                        stringBuilder.append(record.getHtmlText());
                        if(i != count - 1){
                            stringBuilder.append("\n");
                        }
                    }else if(PasteData.MIMETYPE_TEXT_INTENT.equals(mimeType)){
                        HiLog.info(LOG_MAIN, "mimeType = MIMETYPE_TEXT_INTENT");
                        //意图模式
                        Intent intentResult = record.getIntent();
                        String action = intentResult.getStringParam("action");
                        stringBuilder.append("点击跳转至" + action);
                        if(i != count - 1){
                            stringBuilder.append("\n");
                        }
                        text_show.setClickedListener(listen -> {
                            startAbility(intentResult);
                        });
                    }
                }
                if(stringBuilder.length() != 0){
                    HiLog.info(LOG_MAIN, "stringBuilder.length() = " + stringBuilder.length());
                    text_show.setText(stringBuilder.toString());
                    text_show.setMultipleLine(true);
                }

            }
        }else {
            new ToastDialog(getContext())
                    .setText("systemPasteboard is null")
                    .show();
        }
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}